package com.saint.lt.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

import com.saint.lt.domain.Issue;
import com.saint.lt.domain.IssueType;
import com.saint.lt.domain.Priority;
import com.saint.lt.domain.Team;
import com.saint.lt.domain.User;
import com.saint.lt.service.IssueService;
import com.saint.lt.service.UserService;
import com.saint.lt.service.propertyeditors.IssueTypeFromString;
import com.saint.lt.service.propertyeditors.PriorityFromString;
import com.saint.lt.service.propertyeditors.UserFromString;

@Controller
public class LittleTrackerController {

	@Autowired
	private IssueService issueService;
	@Autowired
	private UserService userService;
	
	@RequestMapping("/")
	public String home() {
		return "redirect:/login.jsp";
	}
	
	@RequestMapping("/index")
	public String indexPage() {
		return "home";
	}
	
	@RequestMapping("/newIssue")
	public String newIssuePage() {
		return "newissue";
	}
	
	@RequestMapping("/newTeam")
	public String test() {
		return "newteam";
	}
	
	@RequestMapping(value = "/test/{name}", method = RequestMethod.GET)
	public String test(@PathVariable("name") String name) {
		System.out.println(name);
		
		return "redirect:/login.jsp";
	}
	
	@RequestMapping(value = "/addTeam", method = RequestMethod.POST)
	public String addTeam(@ModelAttribute("team") Team team) {
		System.out.println(team.getMaster().getUsername());
		return "redirect:/index";
	}
	
	@RequestMapping(value = "/addIssue", method = RequestMethod.POST)
	public String addIssue(@ModelAttribute("issue") Issue issue) {
		// TODO: TEMPORAL
		System.out.println(issue.toString());
		//issueService.addIssue(issue);
		return "redirect:/index";
	}
	
	
	@InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
		binder.registerCustomEditor(User.class, new UserFromString(userService));
		binder.registerCustomEditor(Priority.class, new PriorityFromString());
		binder.registerCustomEditor(IssueType.class, new IssueTypeFromString());
    }
}
